/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JOrderStatesPanel.java
 *
 * Created on 2010-4-23, 17:40:26
 */

package cn.ekuma.epos.qkorder.link;

import cn.ekuma.epos.businesslogic.RoleUtil;
import cn.ekuma.epos.qkorder.dialog.JEditMemoDialog;
import cn.ekuma.epos.qkorder.event.OrderChangedEvent;
import cn.ekuma.epos.qkorder.event.OrderChangedListener;
import com.openbravo.bean.AppUser;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderState;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.table.AbstractTableModel;
import org.ofsm.IEvent;
import org.ofsm.impl.FileFiniteStateMachine;

/**
 *
 * @author Administrator
 */
public class JOrderStatesPanel extends javax.swing.JPanel implements OrderChangedListener{
    List<IEvent> ableEvents;
    Order order;
    OrderStateHistoryModel stateHistoryModel;
    FileFiniteStateMachine stateMachine;
    AppUser m_appuser;

    static String[] columns=new String[]{
                         AppLocal.getIntString("orderState.fireDate"),
                         AppLocal.getIntString("orderState.state"),
                         AppLocal.getIntString("orderState.user"),
                         AppLocal.getIntString("orderState.memo")};

    /** Creates new form JOrderStatesPanel */
    public JOrderStatesPanel(AppUser m_appuser,FileFiniteStateMachine stateMachine) {
        initComponents();
        this.m_appuser=m_appuser;
        this.stateMachine=stateMachine;
        ableEvents=new ArrayList();
        stateHistoryModel=new OrderStateHistoryModel();
        jOrderStateHistory.setModel(stateHistoryModel);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEventList = new javax.swing.JList();
        jPanel4 = new javax.swing.JPanel();
        jExeButtom = new javax.swing.JButton();
        jEditMemo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jOrderStateHistory = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jMemo = new javax.swing.JTextPane();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jPanel3.setLayout(new java.awt.BorderLayout());

        jEventList.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("orderState.ableEvents"))); // NOI18N
        jEventList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jEventList.setFocusable(false);
        jEventList.setPreferredSize(new java.awt.Dimension(60, 30));
        jEventList.setRequestFocusEnabled(false);
        jEventList.setVerifyInputWhenFocusTarget(false);
        jScrollPane1.setViewportView(jEventList);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jExeButtom.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        jExeButtom.setText(AppLocal.getIntString("label.run")); // NOI18N
        jExeButtom.setEnabled(false);
        jExeButtom.setFocusable(false);
        jExeButtom.setRequestFocusEnabled(false);
        jExeButtom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jExeButtomActionPerformed(evt);
            }
        });
        jPanel4.add(jExeButtom);

        jEditMemo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/edit.png"))); // NOI18N
        jEditMemo.setText(AppLocal.getIntString("orderState.memo")); // NOI18N
        jEditMemo.setEnabled(false);
        jEditMemo.setFocusable(false);
        jEditMemo.setPreferredSize(new java.awt.Dimension(100, 31));
        jEditMemo.setRequestFocusEnabled(false);
        jEditMemo.setVerifyInputWhenFocusTarget(false);
        jEditMemo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jEditMemoActionPerformed(evt);
            }
        });
        jPanel4.add(jEditMemo);

        jPanel3.add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jPanel1.add(jPanel3);

        add(jPanel1, java.awt.BorderLayout.EAST);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel6.setLayout(new java.awt.BorderLayout());

        jOrderStateHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jOrderStateHistory.setFocusable(false);
        jOrderStateHistory.setPreferredSize(new java.awt.Dimension(350, 64));
        jOrderStateHistory.setRowSelectionAllowed(false);
        jOrderStateHistory.setVerifyInputWhenFocusTarget(false);
        jScrollPane2.setViewportView(jOrderStateHistory);

        jPanel6.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel6, java.awt.BorderLayout.WEST);

        jPanel5.setLayout(new java.awt.BorderLayout());

        jMemo.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("orderState.memo"))); // NOI18N
        jMemo.setEnabled(false);
        jMemo.setFocusCycleRoot(false);
        jMemo.setFocusable(false);
        jMemo.setRequestFocusEnabled(false);
        jMemo.setVerifyInputWhenFocusTarget(false);
        jScrollPane3.setViewportView(jMemo);

        jPanel5.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel5, java.awt.BorderLayout.CENTER);

        add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jExeButtomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jExeButtomActionPerformed
        IEvent select=(IEvent) jEventList.getSelectedValue();
        if(select!=null)
           stateMachine.fireEvent(select,jMemo.getText());
}//GEN-LAST:event_jExeButtomActionPerformed

    private void jEditMemoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jEditMemoActionPerformed
        JEditMemoDialog memoEdit=JEditMemoDialog.getDialog(this, jMemo.getText());
        memoEdit.setVisible(true);
        if(memoEdit.isOk()){
            jMemo.setText(memoEdit.getRetStr());
        }
}//GEN-LAST:event_jEditMemoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jEditMemo;
    private javax.swing.JList jEventList;
    private javax.swing.JButton jExeButtom;
    private javax.swing.JTextPane jMemo;
    private javax.swing.JTable jOrderStateHistory;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables

    @Override
    public void orderChanged(OrderChangedEvent event) {
        order = event.getOrder();
        if(event.isEditAble())
            ableEvents = RoleUtil.filterUserOrderTypeEvents(m_appuser,order.getDefineType().getId(),stateMachine.getTransitionEventByCurrentState());
        jExeButtom.setEnabled(event.isEditAble());
        jEditMemo.setEnabled(event.isEditAble());
        jEditMemo.setText(null);
        reflush();
    }

    private void reflush(){
        jEventList.setModel(new AbstractListModel() {
            public int getSize() {
                return ableEvents.size();
            }

            public Object getElementAt(int index) {
                return ableEvents.get(index);
            }
        });
        stateHistoryModel.fireTableDataChanged();
    }

    class OrderStateHistoryModel extends AbstractTableModel{
        public int getRowCount() {
           if(order==null)
               return 0;
           return order.getStates().size();
        }

        public int getColumnCount() {
            return  columns.length;
        }

        @Override
        public String getColumnName(int column) {
            return columns[column];
        }
        
        public Object getValueAt(int rowIndex, int columnIndex) {
            OrderState state=order.getStates().get(rowIndex);
            switch(columnIndex){
                case 0:
                    return Formats.TIMESTAMP.formatValue(state.getFireDate());
                case 1:
                    return state.getState();
                case 2:
                    return state.getUserName();
                case 3:
                    return state.getMemo();
            }
            return null;
        }
    }
}
